package tech.yilingyi.springdemo.transaction.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.yilingyi.springdemo.transaction.dao.ProductMapper;
import tech.yilingyi.springdemo.transaction.model.Product;
import tech.yilingyi.springdemo.transaction.model.ProductExample;

import java.util.List;

/**
 * Created by yinzuolong on 2016/12/25.
 */
@Service
public class ProductService {

    @Autowired
    private ProductMapper productMapper;

    public int getCount() {
        ProductExample ex = new ProductExample();
        int count = productMapper.countByExample(ex);
        return count;
    }

    public List<Product> queryAllProduct() {
        ProductExample ex = new ProductExample();
        List<Product> products = productMapper.selectByExample(ex);
        return products;
    }

    public void insertProductsNoTransaction(List<Product> products) {
        for (Product product : products) {
            productMapper.insert(product);
        }
    }

    @Transactional
    public void insertProductsTransaction(List<Product> products) {
        for (Product product : products) {
            productMapper.insert(product);
        }
    }

    public void insertProducts(List<Product> products) {
        insertProductsTransaction(products);
    }

    public void deleteProductById(String productId) {
        productMapper.deleteByPrimaryKey(productId);
    }
}
